﻿using System;
using System.Web;
using System.Net.Mail;
using System.Web.UI;
using SimplePMT.Data;
using SimplePMT.Control;
using System.Text;
using System.Collections.Generic;
using Castle.ActiveRecord;
using NHibernate.Expression;
using SimplePMT.Components;

namespace SimplePMT
{
    public partial class TimeReporting : Components.BasePage
    {
        public string StartDate = string.Empty;
        public string EndDate = string.Empty;
        public string PrevStartDate = string.Empty;
        public string PrevEndDate = string.Empty;
        public string NextStartDate = string.Empty;
        public string NextEndDate = string.Empty;
        public string onLoadReport = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(TimeReporting), this.Page);
            ValidateSession();

            DateTime[] dt = Common.GetWeekStartEndDates(DateTime.Now.ToShortDateString());


            if (Request.QueryString["start"] != null && Request.QueryString["end"] != null)
            {
                StartDate = Request.QueryString["start"];
                EndDate = Request.QueryString["end"];
                PrevStartDate = Convert.ToDateTime(Request.QueryString["start"]).AddDays(-7).ToShortDateString();
                PrevEndDate = Convert.ToDateTime(Request.QueryString["start"]).AddDays(-1).ToShortDateString();
                NextStartDate = Convert.ToDateTime(Request.QueryString["start"]).AddDays(7).ToShortDateString();
                NextEndDate = Convert.ToDateTime(Request.QueryString["end"]).AddDays(7).ToShortDateString();
            }

            if (StartDate.Length.Equals(0))
            {
                PrevStartDate = dt[0].AddDays(-7).ToShortDateString();
                PrevEndDate = dt[0].AddDays(-1).ToShortDateString();
                NextStartDate = dt[0].AddDays(7).ToShortDateString();
                NextEndDate = dt[1].AddDays(7).ToShortDateString();
                StartDate = dt[0].ToShortDateString();
                EndDate = dt[1].ToShortDateString();
            }
            

            onLoadReport = GetTimeReport(StartDate, EndDate, false);
        }
        
        [AjaxPro.AjaxMethod()]
        public string GetTimeReport(string fromDate, string toDate, bool summary)
        {
            Control.TimeReportingControl view = this.LoadControl("./Control/TimeReportingControl.ascx") as Control.TimeReportingControl;
            view.FromDate = fromDate;
            view.ToDate = toDate;
            view.ShowSummary = summary;
            return view.ToString();
        }

    }
}
